/**
 * 银联支付回调地址
 * @see [https://open.unionpay.com/tjweb/acproduct/APIList?proId=1#tit7]
 */
import { ICommonParams } from '@feidao/service/interfaces';
import log from '@feidao/web/atom/nodejs/logger/log';

interface IUnionJSON {
	queryId: string;	// 查询流水号	消费交易的流水号，供后续查询用
	currencyCode: string; // 交易币种 默认为156;
	traceTime: string; // 交易传输时间 MMDDhhmmss;
	signature: string; // 签名;
	signMethod: string; // 签名方法;
	settleCurrencyCode: string; // 清算币种
	settleAmt: string; // 清算金额
	settleDate: string; // 清算日期;
	traceNo: string; // 系统跟踪号 ;
	respCode: string; // 应答码
	respMsg: string; // 应答信息
	exchangeDate: string; // 兑换日期件必填; 交易成功; ，交易币种和清算币种不一致的时候返回;  ----
	signPubKeyCert: string; // 签名公钥证书 按条件必填; 使用RSA签名方式时必选; ，此域填写银联签名公钥证书; 。
	exchangeRate: string; // 清算汇率 按条件必填; 交易成功; ，交易币种和清算币种不一致的时候返回; ----
	accNo: string; // 账号AN1.; .1024; 按条件必填; 根据商户配置返回;
	payType: string; // 支付方式N4; 按条件必填; 根据商户配置返回;-----
	payCardNo: string; // 支付卡标识ANS1.; .19; 按条件必填; 移动支付交易时; 根据商户配置返回;----
	payCardType: string; // 支付卡类型 按条件必填; 根据商户配置返回;----
	payCardIssueName: string; // 支付卡名称 按条件必填; 移动支付交易时; ，根据商户配置返回;----
	version: string; // 版本号 需要返回;
	bindId: string; // 绑定标识号  需要返回; 绑定支付时; ，根据商户配置返回;-----
	encoding: string; // 编码方式  UTF - 8; 需要返回;
	bizType: string; // 产品类型  需要返回;
	txnTime: string; // 订单发送时间 YYYYMMDDhhmmss; 需要返回;
	txnAmt: string; // 交易金额  需要返回;
	txnType: string; // 交易类型  需要返回;
	txnSubType: string; // 交易子类  需要返回;
	accessType: string; // 接入类型  需要返回;	0; ：商户直连接入;	1; ：收单机构接入;	2; ：平台商户接入;
	reqReserved: string; // 请求方保留域 需要返回;----
	merId: string; // 商户代码 需要返回;
	orderId: string; // 商户订单号 需要返回;商户订单号; ，不能含; “-; ”或; “_; ”商户自定义; ，同一交易日期内不可重复;商户代码merId; 、商户订单号orderId; 、订单发送时间txnTime三要素唯一确定一笔交易; 。
	reserved: string; // 保留域 查看详情;-----
	accSplitData: string; // 分账域 查看详情;----
}

// 支付处理
async function pay_process(data: IUnionJSON, _spaceid: string) {
	// todo 做支付处理
}

export default async function atom(msg: ICommonParams, action_id: string, session_id: string, _spaceid: string) {
	log('银联支付数据服务开始');
	// {
	// 	"body": {
	// 		"accNo": "6226090000000048",
	// 		"accessType": "0",
	// 		"bizType": "000201",
	// 		"currencyCode": "156",
	// 		"encoding": "UTF-8",
	// 		"merId": "777290058165389",
	// 		"orderId": "1550139291752",
	// 		"queryId": "191902141814510851258",
	// 		"respCode": "00",
	// 		"respMsg": "成功[0000000]",
	// 		"settleAmt": "1",
	// 		"settleCurrencyCode": "156",
	// 		"settleDate": "0214",
	// 		"signMethod": "01",
	// 		"signPubKeyCert": "-----BEGIN CERTIFICATE-----\r\nMIIEQzCCAyugAwIBAgIFEBJJZVgwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UEBhMC\r\nQ04xMDAuBgNVBAoTJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhv\r\ncml0eTEXMBUGA1UEAxMOQ0ZDQSBURVNUIE9DQTEwHhcNMTcxMTAxMDcyNDA4WhcN\r\nMjAxMTAxMDcyNDA4WjB3MQswCQYDVQQGEwJjbjESMBAGA1UEChMJQ0ZDQSBPQ0Ex\r\nMQ4wDAYDVQQLEwVDVVBSQTEUMBIGA1UECxMLRW50ZXJwcmlzZXMxLjAsBgNVBAMU\r\nJTA0MUBaMjAxNy0xMS0xQDAwMDQwMDAwOlNJR05AMDAwMDAwMDEwggEiMA0GCSqG\r\nSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDIWO6AESrg+34HgbU9mSpgef0sl6avr1d\r\nbD/IjjZYM63SoQi3CZHZUyoyzBKodRzowJrw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// 		"traceTime": "0214181451",
	// 		"txnAmt": "1",
	// 		"txnSubType": "01",
	// 		"txnTime": "20190214181451",
	// 		"txnType": "01",
	// 		"version": "5.1.0",
	// 		"signature": "KkYUZMjXzlU8gc+EYIV1vHOg0Khnb78qPVRbsi24RJYzmZo9cvMVnTZAsUS8W88BeRifbfhNjmB2tOMRx83ihO7+vLleIrT9TkGTdlzEYR30jOTsNlIUOY3IP6KQ/4OMtpVzVGntI5FiS7cF5sDT7qRFOm1xakrWJdaa9pPF09TSco7JtB6K7suVagqZTI03VXEgvtPvZ8FLUQIPZ5bcJwJ9dSE6zZrOG59oNxMadWiVXyNKbn0M6V541UUiY5QkHE694cwfzmkSIByQ1LWmCOU4x4Y0AlHO3wnskud40IQ6RSYDpeqIXngejUymKOKs9WGYMaV2LBdKvMucMsBaiw=="
	// 	},
	// 	"cookie": {},
	// 	"data": "",
	// 	"headers": {
	// 		"host": "61.163.79.140:9003",
	// 		"accept": "*/*",
	// 		"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
	// 		"content-length": "2509",
	// 		"expect": "100-continue"
	// 	},
	// 	"params": {},
	// 	"query": {},
	// 	"remote_address": "::ffff:202.101.25.176",
	// 	"url": "/union-payfinish",
	// 	"modelid": "custom::js_union-payfinish-pro",
	// 	"spaceid": "jewelry_mall"
	// }

	const spaceid = msg.spaceid;
	try {
		const data = msg.body as IUnionJSON;
		log('union callback:');
		log(JSON.stringify(data));
		// {
		// 	"accNo": "6226090000000048",
		// 	"accessType": "0",
		// 	"bizType": "000201",
		// 	"currencyCode": "156",
		// 	"encoding": "UTF-8",
		// 	"merId": "777290058165389",
		// 	"orderId": "1550139291752",
		// 	"queryId": "191902141814510851258",
		// 	"respCode": "00",
		// 	"respMsg": "成功[0000000]",
		// 	"settleAmt": "1",
		// 	"settleCurrencyCode": "156",
		// 	"settleDate": "0214",
		// 	"signMethod": "01",
		// 	"signPubKeyCert": "-----BEGIN CERTIFICATE-----\r\nMIIEQzCCAyugAwIBAgIFE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U\r\nmQQLyuqYjES7qKO+zOkzEbvdFwgwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUF\r\nBwMEMA0GCSqGSIb3DQEBBQUAA4IBAQAujhBuOcuxA+VzoUH84uoFt5aaBM3vGlpW\r\nKVMz6BUsLbIpp1ho5h+LaMnxMs6jdXXDh/du8X5SKMaIddiLw7ujZy1LibKy2jYi\r\nYYfs3tbZ0ffCKQtv78vCgC+IxUUurALY4w58fRLLdu8u8p9jyRFHsQEwSq+W5+bP\r\nMTh2w7cDd9h+6KoCN6AMI1Ly7MxRIhCbNBL9bzaxF9B5GK86ARY7ixkuDCEl4XCF\r\nJGxeoye9R46NqZ6AA/k97mJun//gmUjStmb9PUXA59fR5suAB5o/5lBySZ8UXkrI\r\npp/iLT8vIl1hNgLh0Ghs7DBSx99I+S3VuUzjHNxL6fGRhlix7Rb8\r\n-----END CERTIFICATE-----",
		// 	"traceNo": "085125",
		// 	"traceTime": "0214181451",
		// 	"txnAmt": "1",
		// 	"txnSubType": "01",
		// 	"txnTime": "20190214181451",
		// 	"txnType": "01",
		// 	"version": "5.1.0",
		// 	"signature": "KkYUZMjXzlU8gc+EYIV1vHOg0Khnb78qPVRbsi24RJYzmZo9cvMVnTZAsUS8W88BeRifbfhNjmB2tOMRx83ihO7+vLleIrT9TkGTdlzEYR30jOTsNlIUOY3IP6KQ/4OMtpVzVGntI5FiS7cF5sDT7qRFOm1xakrWJdaa9pPF09TSco7JtB6K7suVagqZTI03VXEgvtPvZ8FLUQIPZ5bcJwJ9dSE6zZrOG59oNxMadWiVXyNKbn0M6V541UUiY5QkHE694cwfzmkSIByQ1LWmCOU4x4Y0AlHO3wnskud40IQ6RSYDpeqIXngejUymKOKs9WGYMaV2LBdKvMucMsBaiw=="
		// }
		// 逻辑处理
		pay_process(data, spaceid);

		return { data: 'ok' };
	} catch (error) {
		const err_msg = (error && error.toString()) || 'unknown';
		// 银联支付回调失败
		return { data: err_msg, status_code: 500 };
	}
}
